package com.ruoyi.comm.crud.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.ruoyi.comm.crud.bean.dto.CheckDataDTO;
import com.ruoyi.comm.crud.mapper.IBaseMapper;
import com.ruoyi.comm.crud.service.IBaseService;
import com.ruoyi.common.core.page.PageQO;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.ClzUtils;
import com.ruoyi.common.utils.bean.BeanCopier;
import com.ruoyi.common.utils.bean.PageCopier;

public class BaseImpls<M extends IBaseMapper<T>, T> implements IBaseService<T> {
//	public class BaseImpls<M extends IBaseMapper<T>, T, V> implements IBaseService<T, V> {

    //protected final Class<?>[] typeArguments = GenericTypeUtils.resolveTypeArguments(getClass(), ServiceImpl.class);

    protected final Class<T> entityClass = currentModelClass();

    //protected final Class<B> boClass = currentBoClass();

    //protected final Class<Q> qoClass = currentQoClass();

    @Autowired
    protected M baseMapper;

    //@Override
    public IBaseMapper<T> getBaseMapper() {
    	return baseMapper;
    }

    //@Override
    public Class<T> getEntityClass() {
        return entityClass;
    }

//    protected final Class<M> mapperClass = currentMapperClass();

//    @SuppressWarnings("unchecked")
//	protected Class<M> currentMapperClass() {
//        return (Class<M>) this.typeArguments[0];
//    }

    @SuppressWarnings("unchecked")
    protected Class<T> currentModelClass() {
        return ClzUtils.getSuperClassGenericType(getClass(), 1);
    }

//    @SuppressWarnings("unchecked")
//    protected Class<B> currentBoClass() {
//        return ClzUtils.getSuperClassGenericType(getClass(), 2);
//    }

//    @SuppressWarnings("unchecked")
//    protected Class<Q> currentQoClass() {
//        return ClzUtils.getSuperClassGenericType(getClass(), 3);
//    }

    /**
     * 查询案件类型数据集合
     * 
     * @param post 案件类型信息
     * @return 案件类型数据集合
     */
    @Override
    public List<T> selectList(T t) {
    	return this.baseMapper.selectList(t);
    }
    
    @Override
    public List<T> query(T t, PageQO page) {
        // 如果分页，指定分页参数
//        if (null != page && page.isPagination()) {
//        	PageDUtils.startPage(page);
//            //PageHelper.startPage(pageBean.getPage(), pageBean.getRows());
//        }

    	return this.baseMapper.selectList(t);
    }
    
//    @Override
//    public List<V> queryVO(T u, PageQO page) {
//    	List<T> ls = this.baseMapper.selectList(u);
//    	List<V> list = BeanCopier.copyList(ls, voClass);
//    	return list;
//    }
    
    @Override
    public <A> List<A> query(T t, Class<A> clz, PageQO page) {
    	List<T> ls = this.baseMapper.selectList(t);
    	//PageInfo<T> pageInfo = new PageInfo<>(ls);
    	//BeanCpUtils.copyListProperties(null, null)
    	List<A> list = PageCopier.clone(ls, clz);
    	return list;
    }

    /**
     * 查询所有案件类型
     * 
     * @return 案件类型列表
     */
    @Override
    public List<T> selectAll() {
    	return this.baseMapper.selectAll();
    }

    /**
     * 通过案件类型ID查询案件类型信息
     * 
     * @param postId 案件类型ID
     * @return 角色对象信息
     */
    @Override
    public T selectById(Long id) {
    	return this.baseMapper.selectById(id);
    }
    
    @Override
    public T selectById(String id) {
    	Long lId = Convert.toLong(id, 0L);
    	return this.selectById(lId);
    }

    @Override
    public T selectById(Number id) {
    	Long lId = Convert.toLong(id, 0L);
    	return this.selectById(lId);
    }
    
    @Override
    public T selectByCode(String code) {
    	return this.baseMapper.selectByCode(code);
    }
    
    @Override
    public <A> A selectByCode(String code, Class<A> clz) {
    	T t = this.baseMapper.selectByCode(code);
    	A a = BeanCopier.copyProperties(t, clz);
    	return a;
    }

	@Override
    public List<T> selectListById(Number id) {
    	Long lId = Convert.toLong(id, 0L);
    	return this.baseMapper.selectListById(lId);
    }
	
    @Override
	public <A> List<A> selectListById(Number id, Class<A> clz) {
		List<T> ls = this.selectListById(id);
		List<A> list = BeanCopier.copyList(ls, clz);
		return list;
	}

    /**
     * 批量删除案件类型信息
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteByIds(Long[] ids) {
    	return this.baseMapper.deleteByIds(ids);
    }
    
    @Override
    public int deleteByIds(String ids) {
    	Long[] lsIds = Convert.toLongArray(ids);
    	return this.deleteByIds(lsIds);
    }

    /**
     * 删除案件类型信息
     * 
     * @param id 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteById(Long id) {
    	return this.baseMapper.deleteById(id);
    }

    @Override
    public int deleteById(Number id) {
    	Long l = Convert.toLong(id, 0L);
    	return this.deleteById(l);
    }

    @Override
    public int deleteById(String id) {
    	Long l = Convert.toLong(id, 0L);
    	return this.deleteById(l);
    }

    /**
     * 删除案件类型信息
     * 
     * @param u 案件类型信息
     * @return 结果
     */
    @Override
    public int delete(T u) {
    	return this.baseMapper.delete(u);
    }

    /**
     * 修改案件类型信息
     * 
     * @param u 案件类型信息
     * @return 结果
     */
    @Override
    public int update(T u) {
    	return this.baseMapper.update(u);
    }
    
    @Override
    public int check(T u) {
    	return this.baseMapper.check(u);
    }
    
//    @Override
//    public int check(List<T> list) {
//    	int n = 0;
//    	for (T t : list) {
//    		n += this.check(t);
//    	}
//    	return n;
//    }
    
    @Override
    public int checkByIds(CheckDataDTO u) {
    	return this.baseMapper.checkByIds(u);
    }

    @SuppressWarnings("unchecked")
	@Override
    public <A> int updateA(A a) {
		if (a.getClass() == entityClass) {
			return update((T)a);
    	} else {
    		T t = BeanCopier.copyProperties(a, this.getEntityClass());
    		return update(t);
    	}
    }

    @SuppressWarnings("unchecked")
	@Override
    public <A> int checkA(A a) {
		if (a.getClass() == entityClass) {
			return check((T)a);
    	} else {
    		T t = BeanCopier.copyProperties(a, this.getEntityClass());
    		return check(t);
    	}
    }

    /**
     * 新增案件类型信息
     * 
     * @param post 案件类型信息
     * @return 结果
     */
    @Override
    public int insert(T u) {
    	return this.baseMapper.insert(u);
    }
    
    @Override
    public int insert(List<T> ls) {
    	int n = 0;
    	for (T bean : ls) {
    		n += this.insert(bean);
    	}
    	return n;
    }
    
    @Override
	public <A> int insertA(List<A> ls) {
		List<T> list = BeanCopier.copyList(ls, this.entityClass);
		return this.insert(list);
	}

//    @Override
//    public int insert(Object u) {
//    	return this.baseMapper.insert(u);
//    }
    
//	@SuppressWarnings("unchecked")
//	@Override
//    public <A> int insert(A a, Class<A> clz) {
//    	if (clz == entityClass) {
//			return insert((T)a);
//    	} else {
//    		T t = BeanCopier.copyProperties(a, this.getEntityClass());
//    		return insert(t);
//    	}
//    }
	
	@SuppressWarnings("unchecked")
	@Override
    public <A> int insertA(A a) {
		if (a.getClass() == entityClass) {
			return insert((T)a);
    	} else {
    		T t = BeanCopier.copyProperties(a, this.getEntityClass());
    		return insert(t);
    	}
	}

	@Override
	public int replace(T u) {
    	return this.baseMapper.replace(u);
	}

	@SuppressWarnings("unchecked")
	@Override
	public <A> int replaceA(A a) {
		if (a.getClass() == entityClass) {
			return replace((T)a);
    	} else {
    		T t = BeanCopier.copyProperties(a, this.getEntityClass());
    		return replace(t);
    	}
	}

	@Override
	public int replace(List<T> ls) {
    	int n = 0;
    	for (T bean : ls) {
    		n += this.replace(bean);
    	}
    	return n;
	}

	@Override
	public <A> int replaceA(List<A> ls) {
		List<T> list = BeanCopier.copyList(ls, this.entityClass);
		return this.replace(list);
	}

    /**
     * 校验案件类型名称
     * 
     * @param name 案件类型名称
     * @return 结果
     */
//    @Override
//    public T checkNameUnique(String name) {
//    	return this.baseMapper.checkNameUnique(name);
//    }
//    @Override
//    public T checkNameUnique(T t) {
//    	return this.baseMapper.checkNameUnique(name);
//    }
//
//    @Override
//    public Long checkIdNameUnique(T t) {
//    	return this.baseMapper.checkIdNameUnique(t);
//    }
//    
//    @Override
//	public Boolean existsIdNameUnique(T t) {
//    	Long lExists = this.checkIdNameUnique(t);
//    	if (lExists!=null && lExists.equals(1L)) {
//    		return true;
//    	} else {
//    		return false;
//    	}
//    }
    
    @Override
    public Long getIdByName(T t) {
    	return this.baseMapper.getIdByName(t);
    }
    
    @Override
    public Boolean checkNameUnique(T t) {
    	return false;
    }

}
